home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / Spotlight Hack / source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-21  |  3.9 KB  |  147 lines  |  [TEXT/CWIE]

  1. // main.c
  2.  
  3. ProcInfoType __procinfo = kThinkCStackBased;     // this must match the ProcInfoType
  4.                                                 // used to call this resource in the
  5.                                                 // calling app
  6.  
  7. #include <A4Stuff.h>
  8. #include <CodeFragments.h>
  9.  
  10. #include "main.h"
  11. #include "filter.h"
  12. #include "AppPrefs.h"
  13.  
  14. SpotlightGlobals glob;
  15.  
  16. /*
  17. Various helpful comments.
  18.  
  19. <KeithS> Drop into Macsbug, and type dh 7003 a829
  20. <KeithS> Oh. It's telling you that, in order to call this trap, it's a dispatched trap 
  21. with the dispatch selector in register d0. There's a macro which lets you specify the 
  22. selector word, as I recall.
  23. <DaveK> keith: 3 is the selector word?
  24. <KeithS> Yes.
  25.  
  26. <KeithS> It's at 0x174. Four longs. [the global keymap]
  27.  
  28. <whyte> davek, patch SetTrapAddress...then right before the finder launches the process 
  29. manager will patch WaitNextEvent, you get to insert your patch there
  30.  
  31. <SoMeOnE> DaveK: you would call ADBOP, with listen, Register 0 [to get keyboard info]
  32. */
  33.  
  34. pascal OSErr MyGestaltSelector(OSType selector, long *response);
  35.  
  36. enum {
  37.     uppMainProcInfo = kThinkCStackBased
  38.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(SpotlightGlobals*)))
  39. };
  40.  
  41. typedef    void (*MainProcPtr)(SpotlightGlobals *glob);
  42.  
  43. #if powerc
  44. typedef UniversalProcPtr MainProcUPP;
  45.  
  46. #define CallMainProc(userRoutine, glob)        \
  47.         CallUniversalProc((UniversalProcPtr)(userRoutine), uppMainProcInfo, glob)
  48. #else
  49. typedef MainProcPtr MainProcUPP;
  50.  
  51. #define CallMainProc(userRoutine, glob)        \
  52.         (*(userRoutine))(glob)
  53. #endif
  54.  
  55. void main(void) {
  56.     long saveA4;
  57.     Handle rsrc;
  58.     MainProcUPP mainProc;
  59.     SelectorFunctionUPP mySelector;
  60.     
  61.     EnterCodeResource();
  62.  
  63.     rsrc = Get1Resource('INIT',0);
  64.     DetachResource(rsrc);
  65.     HLock(rsrc);
  66.     
  67.     // this is so the regions etc. we allocate will not be destroyed after our extension loads
  68.     SetZone(SystemZone());
  69.         
  70.     // initialization    
  71.  
  72.     PrefsLoad(&glob.prefs);
  73.     glob.active = glob.activating = glob.deactivating = false;
  74.  
  75.     glob.showSpotlightUPP = NewRoutineDescriptor((ProcPtr)ShowSpotlight, uppShowSpotlightInfo, GetCurrentArchitecture());
  76.     glob.makeSpotlightUPP = NewRoutineDescriptor((ProcPtr)MakeSpotlightRgn, uppMakeSpotlightInfo, GetCurrentArchitecture());
  77.     
  78.     glob.spotlightRgn = NewRgn();
  79.     glob.oldlightRgn = NewRgn();
  80.     glob.newlightRgn = NewRgn();
  81.     glob.scratchRgn = NewRgn();
  82.  
  83.     MakeSpotlightRgn();
  84.  
  85.     // 68k patches
  86.     
  87.     rsrc = Get1Resource('Patc', 128);
  88.     DetachResource(rsrc);
  89.     HLock(rsrc);
  90.     mainProc = (MainProcUPP)*rsrc;
  91.     CallMainProc(mainProc, &glob);
  92.  
  93.     // gestalt selector to interface with app
  94.     
  95.     mySelector = NewSelectorFunctionProc(MyGestaltSelector);
  96.     NewGestalt('liTE', mySelector);
  97.     
  98.     saveA4 = SetA4(0);
  99.     SetA4(saveA4);
  100.     InstallEventFilter((FilterHelperUPP) EventFilterHelper, (Ptr) saveA4);
  101.  
  102.     ExitCodeResource();
  103. }
  104.  
  105. pascal OSErr MyGestaltSelector(OSType selector, long *response) {
  106.     EnterCodeResource();
  107.     *response = (long)&glob;
  108.     ExitCodeResource();
  109.     return noErr;
  110. }
  111.  
  112. void MoveSpotlightOffscreen(void) {
  113.     Rect screenBounds = (*LMGetGrayRgn())->rgnBBox;
  114.     if (screenBounds.top > 0) screenBounds.top = 0; // for menubar
  115.  
  116.     OffsetRgn(glob.spotlightRgn, 
  117.         (screenBounds.left - glob.lightCenter.h) - glob.prefs.spotlightSize/2, 
  118.         (screenBounds.top - glob.lightCenter.v) - glob.prefs.spotlightSize/2);
  119.     glob.lightCenter.h = screenBounds.left - glob.prefs.spotlightSize/2;
  120.     glob.lightCenter.v = screenBounds.top - glob.prefs.spotlightSize/2;
  121. }
  122.  
  123. void MakeSpotlightRgn(void) {
  124.     GrafPtr oldPort, wmgrPort;
  125.     Rect screenBounds, circleRect;
  126.  
  127.     GetPort(&oldPort);
  128.     GetCWMgrPort((CGrafPtr*)&wmgrPort);
  129.     SetPort(wmgrPort);
  130.  
  131.     OpenRgn();
  132.     
  133.     screenBounds = (*LMGetGrayRgn())->rgnBBox;
  134.     if (screenBounds.top > 0) screenBounds.top = 0; // for menubar
  135.     circleRect.top = screenBounds.top;
  136.     circleRect.bottom = screenBounds.top + glob.prefs.spotlightSize;
  137.     circleRect.left = screenBounds.left;
  138.     circleRect.right = screenBounds.left + glob.prefs.spotlightSize;
  139.     
  140.     FrameOval(&circleRect);
  141.     CloseRgn(glob.spotlightRgn);
  142.  
  143.     glob.lightCenter.h = glob.lightCenter.v = glob.prefs.spotlightSize/2;
  144.     MoveSpotlightOffscreen();
  145.  
  146.     SetPort(oldPort);
  147. }